package com.share.example.stock.utils;

import com.share.common.utils.FileUtil;
import com.share.common.utils.StrUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GenTable {
    public static void main(String[] args) {
        List<String> list = FileUtil.readByLineFromCp("tab.tmp");
        StringBuffer sb = new StringBuffer();
        String tableComment = "";
        String columnFlag = "0";
        for (String line : list) {
            //空行
            line = line.trim();
            if (StringUtils.isBlank(line)) {
                continue;
            }

            //创建表-开始字符-识别
            String TABLE_FLAG_STR = "接口:";
            if (line.startsWith(TABLE_FLAG_STR) && line.length() > TABLE_FLAG_STR.length() + 1) {

                if (sb.length() > 0) {
                    sb.append(genEndSql(tableComment));
                    sb.append(System.lineSeparator());
                }

                //1.清理数据
                tableComment = "";
                columnFlag = "";

                //2.提取表面
                String tmpArr[] = line.split(":");
                String tabName = tmpArr[tmpArr.length - 1].trim();

                String startSql = "CREATE TABLE `${tabName}` (";
                startSql = startSql.replace("${tabName}", tabName) + System.lineSeparator();
                startSql += "`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键'," + System.lineSeparator();

                sb.append(startSql); //插入sql
                continue;
            } else if (line.contains("名称") && line.contains("类型") && line.contains("描述")) {
                columnFlag = "1";
                continue;
            }

            if ("1".equals(columnFlag)) {
                String[] split = line.split("\t");
                String column = split[0].trim();
                String type = split[1].trim();
                String show = null;
                String comment = null;
                if (split.length > 3) {
                    show = split[2].trim();
                    comment = split[3].trim();
                } else {
                    comment = split[2].trim();
                }

                if (StrUtil.isContainsChinese(column)) {
                    //中文置换英文名字
                    String tmpCode = columnMap.get(column);

                    //先设置备注
                    if ("-".equals(comment) || StringUtils.isBlank(comment)) {
                        comment = column; //中文覆盖过去
                    }

                    //更换列名
                    if (tmpCode != null) {
                        column = tmpCode;
                    }
                }

                String dbType = "varchar(255)";
                if ("float".equals(type) || "float64".equals(type)) {
                    dbType = "float(12,2)";
                } else if ("str".equals(type) || "object".equals(type)) {
                    dbType = "varchar(255)";
                }

                String columnSql = "`${column}` ${dbType} DEFAULT NULL COMMENT '${comment}',";
                columnSql = columnSql.replace("${column}", column);
                columnSql = columnSql.replace("${dbType}", dbType);
                columnSql = columnSql.replace("${comment}", comment);
                columnSql += System.lineSeparator();

                sb.append(columnSql); // 列语句
            } else {
                tableComment += line + System.lineSeparator();
            }
        }

        if (sb.length() > 0) {
            sb.append(genEndSql(tableComment));
        }

        System.out.println(sb);
    }

    private static String genEndSql(String comment) {
        int comi = comment.lastIndexOf(System.lineSeparator());
        String commentTmp = comment.endsWith(System.lineSeparator()) ? comment.substring(0, comi) : comment;

        String engineTmp = ") ENGINE=InnoDB COMMENT='${tableComment}';";
        engineTmp = engineTmp.replace("${tableComment}", commentTmp);
        String endSql = "PRIMARY KEY (`id`)" + System.lineSeparator();
        endSql += engineTmp + System.lineSeparator();
        return endSql;
    }

    //中文置换英文名字columnMap
    private static Map<String, String> columnMap = new HashMap<>();


    static {
        columnMap.put("成交额", "price");
        columnMap.put("成交额(元)", "price");
        columnMap.put("成交价", "price");
        columnMap.put("成交价格", "price");
        columnMap.put("成交金额(元)", "price");
        columnMap.put("成交量", "volume");
        columnMap.put("成交量(手)", "volume");
        columnMap.put("成交时间", "date");
        columnMap.put("代码", "code");
        columnMap.put("价格变动", "change_price");
        columnMap.put("今开", "open");
        columnMap.put("开盘", "open");
        columnMap.put("流通股本", "floating_stock");
        columnMap.put("流通市值", "floating_price");
        columnMap.put("买入", "buy");
        columnMap.put("卖出", "sell");
        columnMap.put("名称", "name");
        columnMap.put("纳入日期", "nrrq");
        columnMap.put("品种代码", "code");
        columnMap.put("品种名称", "name");
        columnMap.put("日期", "date");
        columnMap.put("收盘", "close");
        columnMap.put("数量(只)", "slz");
        columnMap.put("性质", "xz");
        columnMap.put("涨跌额", "rise_or_fall_price");
        columnMap.put("涨跌幅", "rise_or_fall_rate");
        columnMap.put("5分钟涨跌", "rise_or_fall_rate5m");
        columnMap.put("60日涨跌幅", "rise_or_fall_rate60d");
        columnMap.put("年初至今涨跌幅", "rise_or_fall_price1y");
        columnMap.put("证券类别", "zclb");
        columnMap.put("字典", "zd");
        columnMap.put("总股本", "total_volume");
        columnMap.put("总市值", "total_price");
        columnMap.put("最低", "low");
        columnMap.put("最高", "high");
        columnMap.put("最新价", "last_price");
        columnMap.put("昨收", "yclose");
        columnMap.put("振幅", "amplitude");
        columnMap.put("换手率", "turnover_rate");
        columnMap.put("量比", "quantity_relative_ratio");
        columnMap.put("市净率", "price_to_book_ratio");
        columnMap.put("市盈率-动态", "price_to_book_ratio_dynamic");
        columnMap.put("涨速", "increasing_speed");
        columnMap.put("序号", "seq");
    }
}